<!-- 在这个文件对每个tab对应的列表进行渲染 -->
<template>
	<view class="banner-manger-content">
		<!-- 这里设置了z-paging加载时禁止自动调用reload方法，自行控制何时reload（懒加载）-->
		<!--  :enable-back-to-top="currentIndex===tabIndex" 在微信小程序上可以多加这一句，因为默认是允许点击返回顶部的，但是这个页面有多个scroll-view，会全部返回顶部，所以需要控制是当前index才允许点击返回顶部 -->
		<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" :auto="true">
			<template #top>
				<view class=""  style="background-color: #F8F8F8;">
					<u-navbar :border-bottom="false" title="轮播图管理" :background="{'background':'#F8F8F8'}" :title-size="36" :title-bold="true" :title-color="textColor">
						<template #right>
							<view class="u-p-r-20"  @click="addBanner">
								新增
							</view>	
						</template>
					</u-navbar>
				</view>	
			</template>
			<!-- 如果希望其他view跟着页面滚动，可以放在z-paging标签内 -->
			<view class="banner-container u-m-b-20" v-for="(item,index) in dataList" :key="index" >
				<view class="banner-section u-p-35">
					<view class="banner-section_header u-flex u-col-top">
						<view class="banner-header_lt" style="flex:1;">
							<view class="u-font-32 u-m-b-26" style="font-weight: 500;">{{item.name}}</view>
							<view>
								<text class="u-m-r-20 banner-text" >图片数量</text>
								<text class="banner-text" >{{item.bannerImages.length}}</text>
							</view>
							<view>
								<text class="u-m-r-20 banner-text" >发布策略</text>
								<text class="banner-text" >{{ item.allowedAllRegions?'全部可见':'仅指定企业可见' }}</text>
							</view>
							<view>
								<text class="u-m-r-20 banner-text" >发布状态</text>
								<text class="u-m-r-20 banner-text" >{{ item.isPublished?'已发布':'未发布' }}</text>
								<text class="u-font-28" v-if="item.isPublished" style="color: #ff551a;" @click="toggleStatus(item.id,index)">立即下架</text>
								<text class="u-font-28" v-else style="color: #329B0E;" @click="toggleStatus(item.id,index)">立即上架</text>
							</view>
						</view>
						<view class="banner-header_rt u-flex">
							<view class="u-flex u-col-center u-m-r-40" style="flex-direction: column;" 
							@click="$mRouter.push({route: '/pages/sale/homePageConfig/bannerDetails?id='+item.id })">
								<image src="/static/owners/editContract.png" mode="heightFix" style="height: 46rpx;"></image>
								<text class="u-font-20 u-m-t-4" style="color: #999;" >修改</text>
							</view>
							<view class="u-flex u-col-center" style="flex-direction: column;" @click="delSubmitFn(item.id)">
								<image src="/static/xiaoshou/delPrice.png" mode="heightFix" style="height: 46rpx;"></image>
								<text class="u-font-20 u-m-t-4" style="color: #999;">删除</text>
							</view>
						</view>
					</view>
					<view class="banner-section_content u-m-t-30">
						<u-swiper class="u-skeleton-fillet" :list="item.bannerImages" name="url" :height="304" :border-radius="10"></u-swiper>
					</view>
				</view>
			</view>
			
		</z-paging>
		<akLoading isFullScreen :active="loading"></akLoading>
		<u-modal v-model="delModelShow" confirm-color="#ff551a" @confirm="delBannerFn" :content="'您确定要删除此banner组吗？'" title="注意"></u-modal>
	</view>
</template>

<script>
	import { useShopStore } from '@/store/shop.js';
	import { mapState } from 'pinia';
	export default {
		data() {
			return {
				//v-model绑定的这个变量不要在分页请求结束中自己赋值！！！
				dataList: [],
				loading:false,
				delBannerId:'',
				delModelShow:false,
			}
		},
		computed:{
			...mapState(useShopStore, {storeShop: 'shop'})
		},
		onShow(){
			uni.$off('refreshBanner')//建议先销毁一次监听，再进行新的一次监听，否则会出现重复监听的现象
			let that = this;
			uni.$once('refreshBanner',function(data){
					//这的data就是B页面传递过来的数据
					that.$refs.paging.refresh();
			});
		},
		methods: {
			// 打开删除确认框
			delSubmitFn(id){
				this.delBannerId = id;
				this.delModelShow = true;
			},
			// 删除BANNER
			async delBannerFn(){
				this.loading = true;
				
				let params = `?id=${this.delBannerId}&shopId=${this.storeShop.id}&IsDeleted=false`
				const res = await this.$newHttp.delete('api/v1.2/Seller/DeleteBanner'+params);
				this.loading = false;
				if(res.code == 200){
					this.$u.toast('删除banner成功');
					this.$refs.paging.refresh();
				}else{
					this.$u.toast('修改banner失败');
				}
			},
			// 切换上下架状态
			async toggleStatus(id,index){
				this.loading = true;
				let params = {
					id:id,
					shopId:this.storeShop.id
				}
				const res = await this.$newHttp.put('api/v1.2/Seller/ToggleBannerPublish', params );
				this.loading = false;
				if(res.code == 200){
					this.$u.toast('修改状态成功');
					// this.dataList[index].isPublished = !this.dataList[index].isPublished;
					this.$refs.paging.refresh();
				}else{
					this.$u.toast('修改状态失败');
				}
			},
			reloadFn(){
				this.$refs.paging.reload();
			},
			addBanner(){
				this.$mRouter.push({
					route: '/pages/sale/homePageConfig/bannerDetails' 
				})
			},
			async queryList(pageNo, pageSize) {
				//组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//模拟请求服务器获取分页数据，请替换成自己的网络请求
				const params = {
					pageIndex: pageNo,
					pageSize: pageSize,
					shopId:this.storeShop.id,
					// IsSeller:true
				}
				uni.showLoading({
					title: '加载中...',
					mask: false
				});
				const res = await this.$newHttp.get('api/v1.2/Seller/GetBanners', { params: params } );
				uni.hideLoading({
					title: '加载中...'
				})
				if(res.code === 200){
					this.$refs.paging.complete(res.data.list);
				}else{
					this.$refs.paging.complete(false);
				}
			},
			itemClick(item) {
				console.log('点击了', item.title);
				this.$mRouter.push({ 
					route: '/pages/sale/payorder/createPayOrder?shop='+item.id,
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* 注意:父节点需要固定高度，z-paging的height:100%才会生效 */
	.banner-manger-content {
		height: 100%;
		.banner-container{
			.banner-section{
				background-color: #fff;
				.banner-text{
					font-size: 28rpx;color: #999;
				}
			}
		}
	}


</style>
